﻿namespace FFF.Test.Application.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Abp.Application.Services.Dto;
    using Abp.Domain.Entities;
    using Abp.EntityFramework.Repositories;
    using Abp.EntityFrameworkCore.Repositories;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;
    using FFF.Test.Application.AppServices.PublicService.Grid;
    using FFF.Test.DataAccess.Framework;

    /// <summary>
    /// 仓储基类接口定义一些实体仓储公用方法
    /// </summary>
    /// <typeparam name="TEntity">实体</typeparam>
    /// <typeparam name="TPrimaryKey">主键类型</typeparam>
    /// <typeparam name="TCreateInput">输入实体</typeparam>
    /// <typeparam name="TUpdateInput">输出实体</typeparam>
    /// <typeparam name="TEntityDto">展示实体</typeparam>
    /// <typeparam name="TView">view的类型</typeparam>
    /// <typeparam name="TViewDto">viewDto的展示</typeparam>
    public interface IRepositoriesBase<TEntity, TPrimaryKey, TCreateInput, TUpdateInput, TEntityDto, TView, TViewDto> : IRepositoryWithDbContext
        where TEntity : class, IEntity<TPrimaryKey>
        where TView : class, IEntity<TPrimaryKey>
        where TViewDto : class
        where TCreateInput : class, IEntityDto<TPrimaryKey>
        where TUpdateInput : class, IEntityDto<TPrimaryKey>
        where TEntityDto : class, IEntityDto<TPrimaryKey>
    {
        /// <summary>
        /// 获取MyDbContext
        /// </summary>
        /// <returns>IQueryable&lt;TView&gt;.</returns>
        /// <remarks>于大明 2020-06-03</remarks>
        MyDbContext GetDbContextMy();

        /// <summary>
        /// db的saveChange
        /// </summary>
        /// <returns>影响行数</returns>
        Task<int> SaveChangeAsync();

        /// <summary>
        /// 获取视图对象
        /// </summary>
        /// <param name="isDisableFrameWorkFilter">是否禁用框架的过滤</param>
        /// <returns>获取视图的实例</returns>
        IQueryable<TView> GetView(bool isDisableFrameWorkFilter = false);

        /// <summary>
        /// 获取统计视图
        /// </summary>
        /// <typeparam name="TSummaryView">summaryview的视图</typeparam>
        /// <returns>获取视图的实例</returns>
        IQueryable<TSummaryView> GetSummaryView<TSummaryView>()
            where TSummaryView : class;

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="inDto">导入对象</param>
        /// <returns>提示信息</returns>
        Task<ImportOutDto> ImportExcelAsync(ImportInDto inDto);

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="inDto">导入对象</param>
        /// <param name="dealView">处理view的视图</param>
        /// <returns>提示信息</returns>
        Task<ImportOutDto> ImportExcelAsync(ImportInDto inDto, Action<List<TView>> dealView);

        /// <summary>
        /// 导出表格数据
        /// </summary>
        /// <param name="inDto">获取导出表格数据输入dto</param>
        /// <param name="viewType">默认查询的view的类型</param>
        /// <returns>dto对象</returns>
        Task<FileContentResult> GetGridExcelAsync(GetGridListInDto inDto, Type viewType = null);

        /// <summary>
        /// 获取表格数据
        /// </summary>
        /// <param name="inDto">获取表格数据输入dto</param>
        /// <param name="viewType">默认查询的view的类型</param>
        /// <returns>dto对象</returns>
        Task<GridResult> GetGridDtoAsync(GetGridListInDto inDto, Type viewType = null);

        /// <summary>
        /// 获取表格汇总数据
        /// </summary>
        /// <param name="inDto">输入Dto</param>
        /// <param name="viewType">默认查询的view的类型</param>
        /// <returns>输出dto</returns>
        Task<GetGridSummaryOutDto> GetGridSummaryAsync(GetGridSummaryInDto inDto, Type viewType = null);

        /// <summary>
        /// 获取 流水号 db.GetFlowNumber("CY10")
        /// </summary>
        /// <param name="tableName">表的名称</param>
        /// <returns>表的流水号值</returns>
        string GetFlowNumber(string tableName = null);

        /// <summary>
        /// 获取单个的viewdto对象
        /// </summary>
        /// <param name="primaryKey">获取对象转成dto</param>
        /// <returns>dto对象</returns>
        Task<TViewDto> GetViewDtoAsync(TPrimaryKey primaryKey);

        /// <summary>   
        ///  视图分页
        /// </summary>
        /// <code>
        /// <![CDATA[
        ///  PagedInputDto pagedInputDto = new PagedInputDto()        ///                 {        ///                     PageIndex = 1,        ///                     PageSize = 10,        ///                     Order = "StuName desc"        ///                 };        ///                 pagedInputDto.Filter = new PageFilterDto()        ///                 {        ///                     Type = "and",        ///                     Conditions = new System.Collections.Generic.List<Condition>()        ///                     {        ///                         new Condition() { Attribute = "StuName", Datatype = "nvarchar", Operatoer = "like", Value = "0" },        ///                         new Condition() { Attribute = "Birthday", Datatype = "int", Operatoer = "null" }        ///                     },        ///                     Filters = new System.Collections.Generic.List<PageFilterDto>()        ///                     {        ///                         new PageFilterDto()        ///                         {        ///                             Type = "or",        ///                             Conditions = new System.Collections.Generic.List<Condition>()        ///                             {        ///                                 new Condition() { Attribute = "ApproveState", Datatype = "nvarchar",        ///                                     Operatoer = "eq", Value = "审核中" }        ///                             }        ///                         }        ///                     }        ///                 };        ///                 var pagedResult = service.GetPage(pagedInputDto);
        /// ]]>
        /// </code>
        /// <param name="pagedInputDto">分页输入对象</param>
        /// <returns>分页对象</returns>
        Task<MyPagedResult<TViewDto>> GetViewPageAsync(PagedInputDto pagedInputDto);

        /// <summary>   
        ///  视图分页 通过select查询
        /// </summary>
        /// <code>
        /// <![CDATA[
        ///  PagedInputDto pagedInputDto = new PagedInputDto()        ///                 {        ///                     PageIndex = 1,        ///                     PageSize = 10,        ///                     Order = "StuName desc",        ///                     Select= "new(CompanyName as Name, Phone)"        ///                 };        ///                 pagedInputDto.Filter = new PageFilterDto()        ///                 {        ///                     Type = "and",        ///                     Conditions = new System.Collections.Generic.List<Condition>()        ///                     {        ///                         new Condition() { Attribute = "StuName", Datatype = "nvarchar", Operatoer = "like", Value = "0" },        ///                         new Condition() { Attribute = "Birthday", Datatype = "int", Operatoer = "null" }        ///                     },        ///                     Filters = new System.Collections.Generic.List<PageFilterDto>()        ///                     {        ///                         new PageFilterDto()        ///                         {        ///                             Type = "or",        ///                             Conditions = new System.Collections.Generic.List<Condition>()        ///                             {        ///                                 new Condition() { Attribute = "ApproveState", Datatype = "nvarchar",        ///                                     Operatoer = "eq", Value = "审核中" }        ///                             }        ///                         }        ///                     }        ///                 };        ///                 var pagedResult = service.GetPage(pagedInputDto);
        /// ]]>
        /// </code>
        /// <param name="pagedInputDto">分页输入对象</param>
        /// <returns>分页对象</returns>
        Task<MyPagedResult<object>> GetViewPageSelectAsync(PagedInputDto pagedInputDto);

        /// <summary>   
        ///  分页
        /// </summary>
        /// <code>
        /// <![CDATA[
        ///  PagedInputDto pagedInputDto = new PagedInputDto()        ///                 {        ///                     PageIndex = 1,        ///                     PageSize = 10,        ///                     Order = "StuName desc"        ///                 };        ///                 pagedInputDto.Filter = new PageFilterDto()        ///                 {        ///                     Type = "and",        ///                     Conditions = new System.Collections.Generic.List<Condition>()        ///                     {        ///                         new Condition() { Attribute = "StuName", Datatype = "nvarchar", Operatoer = "like", Value = "0" },        ///                         new Condition() { Attribute = "Birthday", Datatype = "int", Operatoer = "null" }        ///                     },        ///                     Filters = new System.Collections.Generic.List<PageFilterDto>()        ///                     {        ///                         new PageFilterDto()        ///                         {        ///                             Type = "or",        ///                             Conditions = new System.Collections.Generic.List<Condition>()        ///                             {        ///                                 new Condition() { Attribute = "ApproveState", Datatype = "nvarchar",        ///                                     Operatoer = "eq", Value = "审核中" }        ///                             }        ///                         }        ///                     }        ///                 };        ///                 var pagedResult = service.GetPage(pagedInputDto);
        /// ]]>
        /// </code>
        /// <param name="pagedInputDto">分页输入</param>
        /// <returns>分页对象</returns>
        Task<MyPagedResult<TEntityDto>> GetPageAsync(PagedInputDto pagedInputDto);

        /// <summary>
        /// 实体创建异步
        /// </summary>
        /// <param name="input">输入对象</param>
        /// <returns>返回输出对象</returns>
        Task<TEntityDto> CreateByDtoAsync(TCreateInput input);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input">更新输入对象</param>
        /// <returns>主键值</returns>
        Task<TEntityDto> UpdateByDtoAsync(TUpdateInput input);

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id">id值</param>
        /// <returns>假删除返回任务</returns>
        Task DeleteByIdAsync(TPrimaryKey id);

        /// <summary>
        /// 获取表格数据的Query
        /// </summary>
        /// <param name="inDto">The in dto.</param>
        /// <param name="viewType">Type of the view.</param>
        /// <param name="userQuery">用户自定义query对象用于一些特殊的查询</param>
        /// <returns> 表格数据</returns>
        IQueryable<object> GetGridDataQuery(GetGridListInDto inDto, Type viewType, IQueryable<object> userQuery = null);

        /// <summary>
        /// 获取表格数据
        /// </summary>
        /// <param name="inDto">The in dto.</param>
        /// <param name="viewType">Type of the view.</param>
        /// <param name="userQuery">用户自定义query对象用于一些特殊的查询</param>
        /// <returns> 表格数据</returns>
        Task<dynamic> GetGridDataResult(GetGridListInDto inDto, Type viewType, IQueryable<object> userQuery = null);
    }
}
